package com.hd.crdao.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

import com.hd.crdao.R;
import com.hd.crdao.base.BaseSecondActivity;
import com.hd.crdao.utils.ALog;
import com.hd.crdao.utils.GlobalData;
import com.hd.crdao.utils.ImageUtil;
import com.hd.crdao.utils.MyToast;
import com.hd.crdao.view.ExpandHeadView;
import com.hd.crdao.view.MosaicView;

import java.io.File;
import java.io.IOException;

/**
 * 对图片进行编辑的界面
 **/

public class DrawPhotoActivity extends BaseSecondActivity implements OnClickListener, OnSeekBarChangeListener {

    private static final int PHOTO_SAVE_SUCCESS = 104;
    private LinearLayout mMosaicViewContent;
    private MosaicView mMosaicView;
    private ExpandHeadView drawPhotoHead;
    private String imagepath;


    @Override
    protected int setContentView() {
        return R.layout.draw_photo;
    }

    @Override
    protected ExpandHeadView setExpandHeadView() {
        return drawPhotoHead;
    }

    @Override
    protected void initView() {
        mMosaicViewContent = (LinearLayout) findViewById(R.id.draw_photo_view);
        drawPhotoHead = (ExpandHeadView) findViewById(R.id.drawPhoto_head);
        drawPhotoHead.getHeadViewLeftArrow().setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mMosaicView.release();
                finish();
            }
        });
        findViewById(R.id.draw_ok_text).setOnClickListener(this);
        findViewById(R.id.draw_photo_cancel).setOnClickListener(this);
        SeekBar mosaicSizeSeekBar = (SeekBar) findViewById(R.id.seekBar);
        mosaicSizeSeekBar.setOnSeekBarChangeListener(this);
        initData();

    }

    private void initData() {
        imagepath = getIntent().getStringExtra("path");
        // 生成画图视图
        mMosaicView = new MosaicView(DrawPhotoActivity.this);
        mMosaicView.setSourceBitmap(BitmapFactory.decodeFile(imagepath));
        mMosaicViewContent.removeView(mMosaicView);
        mMosaicView.destroyDrawingCache();
        mMosaicViewContent.addView(mMosaicView);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.draw_photo_cancel:
                cancelDrawImage();
                break;
            case R.id.draw_ok_text:
                save2File();        //保存到文件中
                break;

            default:
                break;
        }

    }

    private void save2File() {
        // 新建一个文件保存照片
        File file = new File(GlobalData.tempImagePaht + "/" + System.currentTimeMillis() + ".jpg");
        if (!file.exists()) {
            try {
                if (!new File(GlobalData.tempImagePaht).exists()) {
                    new File(GlobalData.tempImagePaht).mkdirs();
                }
                file.createNewFile();
            } catch (IOException e) {
                ALog.d(e.getMessage());
            }
        }
        if (file.exists()) {
            file.delete();
        }
        try {
            Bitmap saveBitmap = mMosaicView.getMosaicBitmap();
            ImageUtil.saveMyBitmap(file, saveBitmap);// 将图片重新存入SD卡
        } catch (IOException e) {
            ALog.d(e.getMessage());
        }
        mMosaicView.release();
        MyToast.show("保存成功");
        ALog.d("从涂抹发给编辑照片的路径:" + file.getAbsolutePath());
        setResult(PHOTO_SAVE_SUCCESS, new Intent().putExtra("imagepath", file.getAbsolutePath()));
        finish();
    }

    /**
     * 撤销方法
     **/
    public void cancelDrawImage() {
        mMosaicView.destroyDrawingCache();
        Bitmap bitmap = BitmapFactory.decodeFile(imagepath);
        mMosaicView.reset(bitmap);
        if (mMosaicViewContent.getChildCount() == 0) {
            mMosaicViewContent.addView(mMosaicView);
        }
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        mMosaicView.setStrokeMultiples(1 + (float) (progress / 100.0));
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }


    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        int progress = seekBar.getProgress();
        if (progress > 0 && progress < 12.5) {
            seekBar.setProgress(0);
        } else if (progress > 12.5 && progress < 25) {
            seekBar.setProgress(25);
        } else if (progress > 25 && progress < 37.5) {
            seekBar.setProgress(25);
        } else if (progress > 37.5 && progress < 50) {
            seekBar.setProgress(50);
        } else if (progress > 50 && progress < 62.5) {
            seekBar.setProgress(50);
        } else if (progress > 62.5 && progress < 75) {
            seekBar.setProgress(75);
        } else if (progress > 75 && progress < 87.5) {
            seekBar.setProgress(75);
        } else if (progress > 87.5 && progress < 100) {
            seekBar.setProgress(100);
        }
        mMosaicView.setStrokeMultiples(1 + (float) (progress / 50.0));
        mMosaicView.removeStrokeView();

    }


}
